﻿using EMRView;
using EMRView.Extensions.HCAttribute;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EMRView.Extensions.Command.StandardCommand.File
{
    /// <summary>
    /// 将文件生成图片保存到指定得路径
    /// <para>调用该命令需要传入SaveToImageParam类型参数</para>
    /// <para>缺省参数使用：png做为导出图片类型，导出页面模式为单页模式</para>
    /// </summary>
    [Command(CommandType.文件, "SaveToImage", "将文件生成图片保存到指定得路径，所需参数：SaveToImageParam")]
    public class SaveToImageCommand : BaseCommand
    {

        public override Icon CommandIcon
        {
            get
            {
                return Properties.HCResource.生成图片;
            }
        }
        public override object Execute(HCEmrView _view, bool showDefaultUI = false, params object[] para)
        {
            SaveToImageParam param = new SaveToImageParam
            {
                FilePrefix = "",
                ImageType = SaveToImageType.Png,
                OnePaper = false
            };
            if (para != null && para.Length > 0)
            {
                foreach (var item in para)
                {
                    if (item is SaveToImageParam)
                    {
                        param = item as SaveToImageParam;
                    }
                }
            }
            SaveFileDialog vDlg = new SaveFileDialog();
            try
            {
                if (vDlg.ShowDialog() == DialogResult.OK)
                {
                    if (vDlg.FileName != "")
                    {
                        _view.SaveToImage(vDlg.FileName, param.FilePrefix, param.ImageType.ToString(), param.OnePaper);
                    }
                }
            }
            finally
            {
                vDlg.Dispose();
                GC.Collect();
            }
            return null;
        }

        public override void InitControl(object control)
        {
        }
    }

    /// <summary>
    /// 生成并保存图片参数
    /// </summary>
    public class SaveToImageParam
    {
        /// <summary>
        /// 文件前缀，文件名称
        /// </summary>
        public string FilePrefix { get; set; }


        /// <summary>
        /// 文件前缀，文件名称
        /// </summary>
        public SaveToImageType ImageType { get; set; }

        /// <summary>
        /// 是否单图片
        /// </summary>
        public bool OnePaper { get; set; }
    }

    /// <summary>
    /// 图片类型
    /// </summary>
    public enum SaveToImageType
    {
        Png,
        Jpg,
        Bmp,
    }
}
